package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.BLDeviceInfo;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 风扇数据库操作
 */

public class BLDeviceInfoDao {
    private Context mContext;
    private Dao<BLDeviceInfo, Integer> mDao;
    private DatabaseHelper helper;

    public BLDeviceInfoDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            mDao = helper.getDao(BLDeviceInfo.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(BLDeviceInfo deviceInfo) {
        try {
            mDao.create(deviceInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加或更新
     *
     * @param deviceInfo
     */
    public void addOrUpdate(BLDeviceInfo deviceInfo) {
        if (deviceInfo == null) {
            return;
        }
        String uuid = deviceInfo.getDid();
        try {
            BLDeviceInfo device = getByUuid(uuid);
            if (device == null) {
                int i = mDao.create(deviceInfo);
                Log.i("LFX", "addOrUpdate: create = "+i+" \n"+deviceInfo.toString());
            } else {
                device.setFamilyId(deviceInfo.getFamilyId());
                device.setKey(deviceInfo.getKey());
                device.setTerminalId(deviceInfo.getTerminalId());
                device.setBlType(deviceInfo.getBlType());
                int update = mDao.update(device);
                Log.i("LFX", "addOrUpdate: update = "+update+" \n"+device.toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public BLDeviceInfo get(int id) {
        try {
            return mDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public BLDeviceInfo getByUuid(String uuid) {
        List<BLDeviceInfo> deviceInfoList = null;
        try {
            deviceInfoList = mDao.queryBuilder().where().eq("did", uuid).query();
            if (deviceInfoList != null && deviceInfoList.size() > 0) {
                return deviceInfoList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * @return
     */
    public List<BLDeviceInfo> getAllBLDeviceInfo() {
        List<BLDeviceInfo> deviceInfoList = new ArrayList<>();
        try {
            deviceInfoList = mDao.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceInfoList;
    }


    public List<BLDeviceInfo> getFamilyBLDeviceInfos(String familyId) {
        List<BLDeviceInfo> deviceInfoList = new ArrayList<>();
        try {
            deviceInfoList = mDao.queryBuilder().where().eq("familyId", familyId).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceInfoList;
    }

    public List<BLDeviceInfo> getFamilyBLDeviceInfos(String familyId, int blType) {
        List<BLDeviceInfo> deviceInfoList = new ArrayList<>();
        try {
            deviceInfoList = mDao.queryBuilder().where()
                    .eq("familyId", familyId)
                    .and()
                    .eq("blType", blType)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceInfoList;
    }
    public int delete(BLDeviceInfo deviceInfo) {
        BLDeviceInfo info = getByUuid(deviceInfo.getDid());
        try {
            if (info != null) {
                return mDao.delete(info);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            mDao.deleteBuilder().delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<BLDeviceInfo> deviceInfoList) {
        try {
            int number = mDao.delete(deviceInfoList);
            LogHelper.i("delete BLDeviceInfo number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void deleteFamilyBLDeviceInfos(String familyId) {
        try {
            List<BLDeviceInfo> deviceInfoList = getFamilyBLDeviceInfos(familyId);
            if (deviceInfoList == null || deviceInfoList.size() < 1) {
                return;
            }
            int number = mDao.delete(deviceInfoList);
            LogHelper.i("deleteFamilyBLDeviceInfos number == " + number);
            for (BLDeviceInfo blDeviceInfo : deviceInfoList) {
                LogHelper.i("\n deleteFamilyBLDeviceInfos blDeviceInfo == " + blDeviceInfo.toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /*** 根据blType获取设备*/
    public List<BLDeviceInfo> getTypeDevice(int blType) {
        List<BLDeviceInfo> deviceInfos = new ArrayList<>();
        try {
            deviceInfos = mDao.queryBuilder().where()
                    .eq("blType", blType)
                    .query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return deviceInfos;
    }

    public void update(BLDeviceInfo deviceInfo) {
        if (deviceInfo == null) {
            return;
        }
        try {
            int update = mDao.update(deviceInfo);
            Log.i("LFX", "update BLDeviceInfo: " + deviceInfo.getName() + " result = " + update);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void update(List<BLDeviceInfo> blDeviceInfos) {
        if (blDeviceInfos == null || blDeviceInfos.size() < 0) {
            return;
        }
        try {
            for (BLDeviceInfo deviceInfo : blDeviceInfos) {
                int update = mDao.update(deviceInfo);
                Log.i("LFX", "update BLDeviceInfo: " + deviceInfo.getName() + " result = " + update);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
